Conditions | 1 |
Paths | 1 |
Total Lines | 14 |
Code Lines | 11 |
Lines | 14 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | const MULTIPLY_BASE = 1000 |
||
6 | View Code Duplication | uniqueIdOfToday: async function (serverId = 1) { |
|
|
|||
7 | let timebase = new Date(new Date().setHours(0, 0, 0, 0)).getTime() |
||
8 | let time = Date.now() + Math.random() // 没有锁,增加随机因子 |
||
9 | |||
10 | let elapse = (time - timebase) * MULTIPLY_BASE // 阿里云执行保证不重复 |
||
11 | let elapseBin = (parseInt(Math.pow(2, 37) - 1) + parseInt(elapse)).toString(2) // 38bit |
||
12 | let serverIdBin = (Math.pow(2, 4) - 1 + serverId).toString(2) // 5bit // 1 to 2^4-1 ,即最大支持15台服务器 |
||
13 | let random = parseInt(Math.random() * (Math.pow(2, 6) - 1 - 1 + 1) + 1, 10) // |
||
14 | let randomBin = (Math.pow(2, 6) - 1 + random).toString(2) // 7bit |
||
15 | let resultBin = elapseBin + serverIdBin + randomBin // 50bit |
||
16 | // let result = await General.bindec(resultBin) |
||
17 | let result = parseInt(resultBin, 2) |
||
18 | return result |
||
19 | }, |
||
20 | |||
28 | } |